package com.zrwl.poscloud.core.restapi.admin.baseInfo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.base.entity.ServerSyncRecord;
//import com.zrwl.poscloud.base.entity.ServerSyncVersion;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.validator.group.Delete;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.xo.service.ServerSyncRecordService;
import com.zrwl.poscloud.xo.vo.ServerSyncRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务器业务同步记录 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2022-03-26
 */
@RestController
@Api(value = "服务器业务同步记录相关接口", tags = {"服务器业务同步记录相关接口"})
@RequestMapping("/serverSyncRecord")
@Slf4j
public class ServerSyncRecordRestApi {
    @Autowired
    private ServerSyncRecordService serverSyncRecordService;

    @AuthorityVerify
    @ApiOperation(value = "获取服务器业务同步记录列表", notes = "获取服务器业务同步记录列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody ServerSyncRecordVO serverSyncRecordVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取服务器业务同步记录列表: {}", serverSyncRecordVO);
        return ResultUtil.successWithData(serverSyncRecordService.getPageList(serverSyncRecordVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取服务器业务同步记录列表", notes = "获取服务器业务同步记录列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody ServerSyncRecordVO serverSyncRecordVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取服务器业务同步记录列表: {}", serverSyncRecordVO);
        return ResultUtil.successWithData(serverSyncRecordService.getAllList(serverSyncRecordVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "新增服务器业务同步记录")
    @ApiOperation(value = "新增服务器业务同步记录", notes = "新增服务器业务同步记录", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody ServerSyncRecordVO serverSyncRecordVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增服务器业务同步记录: {}", serverSyncRecordVO);
        return serverSyncRecordService.add(serverSyncRecordVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "修改服务器业务同步记录")
    @ApiOperation(value = "修改服务器业务同步记录", notes = "修改服务器业务同步记录", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody ServerSyncRecordVO serverSyncRecordVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("修改服务器业务同步记录: {}", serverSyncRecordVO);
        return serverSyncRecordService.edit(serverSyncRecordVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "删除服务器业务同步记录")
    @ApiOperation(value = "删除服务器业务同步记录", notes = "删除服务器业务同步记录", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody ServerSyncRecordVO serverSyncRecordVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("删除服务器业务同步记录: {}", serverSyncRecordVO);
        return serverSyncRecordService.delete(serverSyncRecordVO);
    }

    //    @AuthorityVerify
//    @OperationLogger(value = "云端处理同步业务记录")
//    @ApiOperation(value = "云端处理同步业务记录", notes = "云端处理同步业务记录", response = String.class)
//    @PostMapping("/processSyncRecordDataFromServer")
//    public String processSyncRecordDataFromServer(ServerSyncRecordVO serverSyncRecordVO) {
//        log.info("云端处理同步业务记录: {}", serverSyncRecordVO);
//        Map<Integer, JSONArray> sourceMap = JSONObject.parseObject(serverSyncRecordVO.getJsonStr(), Map.class);
//        Map<Integer, List<ServerSyncRecord>> resultMap = new HashMap<>();
//        sourceMap.forEach((key, value) -> {
//            List<ServerSyncRecord> recordList = value.toJavaList(ServerSyncRecord.class);
//            resultMap.put(key, recordList);
//        });
//        return serverSyncRecordService.processSyncRecordDataFromServer(resultMap);
//    }

    //    @AuthorityVerify
//    @OperationLogger(value = "服务器获取云端同步业务记录")
//    @ApiOperation(value = "服务器获取云端同步业务记录", notes = "服务器获取云端同步业务记录", response = String.class)
//    @PostMapping("/downloadSyncDataFromCloud")
//    public String downloadSyncDataFromCloud(ServerSyncRecordVO serverSyncRecordVO) {
//        log.info("服务器获取云端同步业务记录: {}", serverSyncRecordVO);
//        JSONArray jsonArray = JSONArray.parseArray(serverSyncRecordVO.getJsonStr());
//        List<ServerSyncVersion> sourceList = jsonArray.toJavaList(ServerSyncVersion.class);
//        serverSyncRecordService.updateSyncStatusByServerTypeLastTime(sourceList);
//        Map<Integer, List<ServerSyncRecord>> sourceMap = serverSyncRecordService.getServerSyncDataList(sourceList);
//        if (sourceMap != null) {
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("resultMap", JSONObject.toJSONString(sourceMap));
//            return ResultUtil.successWithData(JSONObject.toJSONString(jsonObject));
//        } else {
//            return ResultUtil.successWithData(null);
//        }
//    }

}
